function setdropdown(newOptions, elementID, selectedID) {
	var select = $(elementID);
	var options = select.attr('options');
	$.each(newOptions, function(val, text) {		
		options[options.length] = new Option(text, val);				
	});
	
	if(selectedID != "" && typeof(selectedID) != "undefined") {
		$(elementID).children().each(function(){
		    if ($(this).val() == selectedID){					        
		        $(this).attr("selected","true"); 
		    }
		});
	}
}

function setRadioBoxValue(jasonData,elementID,selectedValue){
	var tag='input[name="'+elementID+'"]';	
	$.each(jasonData, function(val, text) {	
		$(tag).each(function(){
			if($(this).attr("title")==text){
				$(this).attr('value', val);
			}
		});
	});
	setRadioBox(elementID, selectedValue);
};

function setdropdownByID(newOptions, elementID) {
	var selectedId;
	$.each(newOptions[1], function(val, text) {
		selectedId=val;
	});
	setdropdown(newOptions[0], elementID);
	setSelectedOption(elementID, selectedId);	
}

function setSelectedOption(elementID, selectedId) {
	$(elementID).children().each(function() {
		if (this.value == selectedId) {
			$(this).attr("selected", "true");
		}
	})

}

function setRadioBox(elementID,selectedVal){
	var tag='input[name="'+elementID+'"]';
	$(tag).each(function() {
		if ($(this).val() == selectedVal) {
			$(this).attr("checked", "true");
		}
	})
}

function setCheckBox(){
	var checkboxList = $("#" + $(this).closest("table").attr("id") + " input[type='checkbox']");
	var checked_status = $(this).attr("checked");

	checkboxList.each(function(){
		$(this).attr("checked", checked_status);
	});	
}

function loadTemplate(divElementID,templateURL,tableElementID,checkAllelementID){
	$(divElementID).setTemplateURL(templateURL, [], {
		filter_data : false
	});
	$(divElementID).processTemplate("");
    $(tableElementID+" input[name='"+checkAllelementID+"']").click(setCheckBox);
}

function loadTableData(divElementID,tableElementID,actionURL){
	var loadType=0;
	var chkBoxName="";
	if(divElementID=="#personalList"){
		loadType=0;
		chkBoxName="personalTableCheckbox";
	}else if(divElementID=="#allList"){
		loadType=1;
		chkBoxName="allTableCheckbox";
	}
	 $(tableElementID).dataTable({
			"bAutoWidth": false,
			"bJQueryUI" : true,		
			"bLengthChange": true,
			"bProcessing": true,
			"bServerSide": true,
			"sPaginationType" : "full_numbers",
			"iDisplayLength": 25,            				
			"aoColumnDefs" : [{"bSortable": false, "aTargets": [ 0 ]}],
			"oLanguage": {"sInfoFiltered": ""},
			"sAjaxSource": actionURL+"?loadType="+loadType,            				
			"fnRowCallback":function( nRow, aData, iDisplayIndex ){            					      						
				var num=nRow.cells.length;
				var mailAddress;
				for(var i=0;i<num;i++) {
					if(i==0){
						// checkbox
						$(nRow.cells[i])[0].align="center";
						$(nRow.cells[i]).append(
							$(document.createElement("input")).attr({
								type:'checkbox',
								name:chkBoxName,
								value:$(nRow.cells[1])[0].innerText,
							})
						)
						continue;
					}else if(i==1||i==3){
						// talent id
						$(nRow.cells[i]).css('display','none');
						$(nRow.cells[i]).css('visibility','hidden');
					}else if(i==7){
						// email address
						mailAddress=$(nRow.cells[i])[0].innerText;
						var mailAddressSubStr;
						if (mailAddress != undefined) {
							if (mailAddress.length > 10) {
								mailAddressSubStr = mailAddress.substring(0, 10) + "...";
							} else {
								mailAddressSubStr = mailAddress;
							}
						}
						$(nRow.cells[i])[0].innerText="";
						$(nRow.cells[i]).append(
							$(document.createElement("a")).attr({
								href:'mailto:'+mailAddress,
								innerHTML:mailAddressSubStr
							})               							               								
						)	
					}else if(i==8){
						var expertise=$(nRow.cells[i])[0].innerText;
						if(expertise.length > 40){
							$(nRow.cells[i])[0].innerText=expertise.substring(0, 40) + "...";
						}else{
							$(nRow.cells[i])[0].innerText=expertise;
						}
					}
					$(nRow.cells[i]).click(function(event){
						window.location.replace("talentInfo.jsp?talentID="
								+$(nRow.cells[1])[0].innerText);
					});
				}
				return nRow;
			}
		});  
}

function updateTableTemplate(sjson, divElementID, templateURL, tableElementID,
		hideSearch, rowFunction, checkAllelementID) {
	// Set URL of the template. Setting filter_data to false - prevents HTML
	// encode
	$(divElementID).setTemplateURL(templateURL, [], {
		filter_data : false
	});
	$(divElementID).processTemplate(sjson);

	$(tableElementID + ' td').click(rowFunction);

	var table = $(tableElementID).dataTable({
		"bAutoWidth": false,
		"bJQueryUI" : true,		
		"bLengthChange": true,
		"iDisplayLength": 25,
		"sPaginationType" : "full_numbers",
		"aoColumnDefs" : [{"bSortable": false, "aTargets": [ 0 ]}],
		"aaSorting": []
	});

	if (hideSearch == true) {
		$('.dataTables_filter').hide();
	}
	
	// set checkbox checked and unchecked
	var actionIcon = $(tableElementID+" input[name='"+checkAllelementID+"']");
	actionIcon.click(setCheckBox);

	return table;
}

function IsValidEmail(email){	
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;	
	return filter.test(email);
}

// function to avoid EL param setting in JS functions causing JS error
function getFromAttribute(attributeValue) {
	if (!attributeValue) {
		return 0;
	}
	return attributeValue;
}

function getCheckedArray(templatID,chkBoxName){
	 var idAry = [];
	$(templatID+" input:checkbox[name='"+chkBoxName+"']").each(function(){
		if($(this).is(":checked")){
			idAry.push($(this).val());
		}
	});
	return idAry;
}

function summiterAction(inputTagID){
	$('input').keypress(function(e) {
		  if (e.keyCode == '13') {
			  e.preventDefault();
			  if(!inputTagID){
				  // return false;
			  }else{
				  $(inputTagID).click();
			  }		  
		   }
		});
}

function joinInfoForms(fromForm, toForm) {
	$(fromForm + " :input[isacopy]").remove();
	$(fromForm + " :input").not("[type='button']").clone().hide().appendTo(toForm);
	
	$(fromForm + " textarea").each(function() {
		if($(this).attr("id") != "") {
			$(toForm + " #" + $(this).attr("id")).val($(this).val());
		}
	});
	
	$(fromForm + " select").each(function() {
		if($(this).attr("id") != "") {
			$(toForm + " #" + $(this).attr("id")).val($(this).val());
		}
	});
}
